package com.mao.jxc.admin.controller;


import com.mao.jxc.admin.exceptions.ParamsException;
import com.mao.jxc.admin.model.RespBean;
import com.mao.jxc.admin.pojo.User;
import com.mao.jxc.admin.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author mao
 * @since 2021-07-20
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    // 实现用户登录功能,使用springSecurity组件
    @RequestMapping("/login")
    @ResponseBody
    public RespBean login(@RequestParam("userName") String userName,@RequestParam("password") String password, HttpSession session){

        User user = userService.login(userName,password);
        session.setAttribute("user",user);
        return RespBean.success("用户登录成功");

    }

    /**
     * 跳转到更新用户信息页面
     * @param session
     * @return
     */
    @RequestMapping("/setting")
    public String setting(HttpSession session){
        User user = (User) session.getAttribute("user");
        User refreshedUser = userService.getById(user.getId());
        session.setAttribute("user",refreshedUser);
        return "user/setting";

    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public RespBean updateUserInfo(User user){

        userService.updateUserInfo(user);
        return RespBean.success("更新用户信息成功");
     }


    /**
     * 跳转到修改密码页面
     * @return
     */
     @RequestMapping("/toPasswordPage")
     public String password(){
        return "user/password";
     }


    /**
     * 修改密码
     * @param oldPassword
     * @param newPassword
     * @param confirmPassword
     * @param session
     * @return
     */
     @RequestMapping("/updateUserPassword")
     @ResponseBody
     public RespBean updatePassword(@RequestParam("oldPassword") String oldPassword,
                                    @RequestParam("newPassword") String newPassword,
                                    @RequestParam("confirmPassword") String confirmPassword,
                                    HttpSession session){
         User user = (User) session.getAttribute("user");
         String userName = user.getUserName();
         userService.updatePassword(oldPassword,newPassword,confirmPassword,userName);
         return RespBean.success("密码更新成功!");

     }

}
